package com.mike.controller.backend;

import com.mike.common.Const;
import com.mike.common.ResponseCode;
import com.mike.common.ServerResponse;
import com.mike.pojo.User;
import com.mike.service.ICategoryService;
import com.mike.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by HP on 2018/3/3.
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageAction {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 验证用户具有管理员限权，追加目录节点
     * param session
     * param categoryName
     * param parentId 通过@RequestParam注解配置默认值为0
     */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session , String categoryName, @RequestParam(value = "parentId",defaultValue = "0")int parentId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if(user == null){
            return ServerResponse.createErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");

        }
        //校验是否具有管理员限权
        if(userService.checkAdminRole(user).isSuccess()){
            //如果是管理员，调用categoryService实现对品类节点的添加
            return categoryService.addcategory(categoryName,parentId);
        }else{
            return ServerResponse.createErrorMessageResponse("无限权操作，需要管理员限权");
        }

    }


    /**
     * 具有管理员限权，修改品类名字
     * param session
     * param categoryId
     * param categoryName
     * return
     */
    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");

        }
        if(userService.checkAdminRole(user).isSuccess()){
            //更新categoryName
            return categoryService.updateCategoryName(categoryId,categoryName);
        }else{
            return ServerResponse.createErrorMessageResponse("无限权操作，需要管理员限权");
        }


    }


    /**
     * 根据categoryId，获取平级子节点
     * param session
     * param categoryId
     * return
     */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(
            HttpSession session,
            @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登录");
        }
        if (userService.checkAdminRole(user).isSuccess()) {
            //查询子节点的category信息，平级
            return categoryService.getChildrenParallelCategory(categoryId);

        } else {
            return ServerResponse.createErrorMessageResponse("无权限操作，需要管理员限权");
        }
    }


    /**
     * 具有管理员权限，获得当前id及所有递归子节点
     * param session
     * param categoryId
     * return
     */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,
                                                             @RequestParam(value ="categoryId",defaultValue ="0") Integer categoryId){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        if(userService.checkAdminRole(user).isSuccess()){
            //查询当前节点的id和递归子节点的id
            //1001点 ->100101电脑配件 -> 10010101显示器  品类逐级深入遍历查询
            return categoryService.selectCategoryAndChildrenById(categoryId);
        }else{
            return ServerResponse.createErrorMessageResponse("无权限操作，需要管理员限权");
        }
    }

}
